/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include <common.h>
#include <device/map.h>
#include <SDL2/SDL.h>

enum {
  reg_freq,
  reg_channels,
  reg_samples,
  reg_sbuf_size,
  reg_init,
  reg_count,
  nr_reg
};

static uint8_t *sbuf = NULL;
static uint32_t *audio_base = NULL;

static int rptr = 0;
static SDL_AudioSpec s = {};

static void call_back(void *userdata, uint8_t *stream, int len) {
  int nread = audio_base[reg_count];

  if (nread < len) {
    for (int i = nread; i < len; i++) {
      stream[i] = 0;
    }
  }
  else nread = len;

  for (int i = 0; i < nread; i++) {
    stream[i] = sbuf[rptr + i];
  }
  
  rptr = (rptr + nread) % CONFIG_SB_SIZE;
  audio_base[reg_count] = audio_base[reg_count] - nread;
  audio_base[reg_sbuf_size] = audio_base[reg_sbuf_size] + nread;
}

static void init_audio_subsystem(SDL_AudioSpec *s, int freq, uint8_t channels, uint16_t samples) {
  audio_base[reg_sbuf_size] = CONFIG_SB_SIZE;

  s->format = AUDIO_S16SYS;
  s->userdata = NULL;
  s->freq = freq;
  s->channels = channels;
  s->samples = samples;
  s->callback = call_back;

  SDL_InitSubSystem(SDL_INIT_AUDIO);
  SDL_OpenAudio(s, NULL);
  SDL_PauseAudio(0);
}

static void audio_io_handler(uint32_t offset, int len, bool is_write) {
  if (audio_base[reg_init] == 1) {init_audio_subsystem(&s, audio_base[reg_freq], audio_base[reg_channels], audio_base[reg_samples]);
    audio_base[reg_init] = 0;
  }
}

void init_audio() {
  uint32_t space_size = sizeof(uint32_t) * nr_reg;
  audio_base = (uint32_t *)new_space(space_size);
#ifdef CONFIG_HAS_PORT_IO
  add_pio_map ("audio", CONFIG_AUDIO_CTL_PORT, audio_base, space_size, audio_io_handler);
#else
  add_mmio_map("audio", CONFIG_AUDIO_CTL_MMIO, audio_base, space_size, audio_io_handler);
#endif

  sbuf = (uint8_t *)new_space(CONFIG_SB_SIZE);
  add_mmio_map("audio-sbuf", CONFIG_SB_ADDR, sbuf, CONFIG_SB_SIZE, NULL);
}
